package com.ghost.alibabaghost.utils;

import java.io.InputStream;
import java.net.URL;

/**
 * 资源工具
 * @author Michael
 * @date 2014年1月23日 上午10:48:23
 * @version 3.0
 */
public final class ResourceUtil {

	/**
	 * 从类路径下加载资源文件
	 * @param classpath
	 * @return
	 */
	public static InputStream getResourceAsStream(String classpath)
	{
		InputStream in = null;
		if(!classpath.startsWith("/")) classpath = "/" + classpath;
		
		in = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpath);
		if(in == null)
		{
			in = ResourceUtil.class.getClassLoader().getResourceAsStream(classpath);
		}
		
		if(in == null)
		{
			in = ResourceUtil.class.getResourceAsStream(classpath);
		}
		
		return in;
	}
	
	/**
	 * 从类路径下加载资源文件
	 * @param classpath
	 * @return
	 */
	public static URL getResource(String classpath)
	{
		URL url = null;
		if(!classpath.startsWith("/")) classpath = "/" + classpath;
		
		url = Thread.currentThread().getContextClassLoader().getResource(classpath);
		if(url == null)
		{
			url = ResourceUtil.class.getClassLoader().getResource(classpath);
		}
		
		if(url == null)
		{
			url = ResourceUtil.class.getResource(classpath);
		}
		
		return url;
	}
	
}
